from django.db import transaction
from rest_framework import serializers

from goods.models import SKUImage, SKU, GoodsVisitCount, GoodsCategory, Goods, SpecificationOption, GoodsSpecification, \
    SKUSpecification


class ImageSeriazlier(serializers.ModelSerializer):
    sku = serializers.StringRelatedField(label='商品')

    class Meta:
        model = SKUImage
        fields = ('id', 'sku', 'image')
        read_only_fields = ('id',)


class SKUSeriazlier(serializers.ModelSerializer):
    class Meta:
        model = SKU
        fields = ('id', "name")


class SKUCountSerializer(serializers.ModelSerializer):
    class Meta:
        model = GoodsVisitCount
        fields = ("category", "count")


class SKUCategorieSerializer(serializers.ModelSerializer):
    class Meta:
        model = GoodsCategory
        fields = ('id', 'name')


class GoodsSimpleSerializer(serializers.ModelSerializer):
    class Meta:
        model = Goods
        fields = ('id', 'name')


class GoodsOptineSerializer(serializers.ModelSerializer):

    class Meta:
        model = SpecificationOption
        fields = ('id', 'value')


class GoodsSpecSerialzier(serializers.ModelSerializer):
    id = serializers.IntegerField(label='规格id')
    name = serializers.StringRelatedField(label='规格名称')
    spu = serializers.StringRelatedField(label='SPU商品名称')
    spu_id = serializers.IntegerField(label='SPU商品id')

    options = GoodsOptineSerializer(read_only=True, many=True)  # 使用规格选项序列化器

    class Meta:
        model = GoodsSpecification  # SPUSpecification中的外键spu关联了SPU商品表
        exclude = ('create_time', 'update_time')  # 排除掉的字段
